home *** CD-ROM | disk | FTP | other *** search
/ Universität Tübingen - 1997/1998 Winter / Universität Tübingen - Wintersemester 1997-98 - Uni-Informationssystem und Stadt-Informationssystem.iso / ajs / imageloo.jav < prev    next >
Text File  |  1997-08-14  |  5KB  |  194 lines

  1. /*
  2.  * @(#)ImageLoopItem.java    1.22f 95/03/28 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.io.InputStream;
  21. import java.awt.*;
  22. import java.net.*;
  23.  
  24. /**
  25.  * ImageLoop class. This is a container for a list
  26.  * of images that can be animated.
  27.  *
  28.  * @author     James Gosling
  29.  * @version     1.22f, 28 Mar 1995
  30.  */
  31. class ImageLoop {
  32.     /**
  33.      * The images.
  34.      */
  35.     Image imgs[];
  36.  
  37.     /**
  38.      * The number of images actually loaded.
  39.      */
  40.     int nimgs = 0;
  41.  
  42.     /**
  43.      * Load the images, from dir. The images are assumed to be
  44.      * named T1.gif, T2.gif...
  45.      * Once all images are loaded the applet is resized to the
  46.      * maximum size().width and size().height.
  47.      */
  48.     ImageLoop(URL context, String dir, ImageLoopItem parent, int nimgs) {
  49.     this.nimgs = nimgs;
  50.     imgs = new Image[nimgs];
  51.     for (int i = 0; i < nimgs; i++) {
  52.         imgs[i] = parent.getImage(parent.getCodeBase(), dir + "/T" + (i + 1) + ".gif");
  53.     }
  54.     }
  55. }
  56.  
  57. /**
  58.  * A simple Item class to play an image loop.  The "img" tag parameter
  59.  * indicates what image loop to play.
  60.  *
  61.  * @author     James Gosling
  62.  * @version     1.22f, 28 Mar 1995
  63.  */
  64. public class ImageLoopItem extends java.applet.Applet implements Runnable {
  65.     /**
  66.      * The current loop slot.
  67.      */
  68.     int loopslot = 0;
  69.  
  70.     /**
  71.      * The directory or URL from which the images are loaded
  72.      */
  73.     String dir;
  74.  
  75.     /**
  76.      * The image loop.
  77.      */
  78.     ImageLoop loop;
  79.  
  80.     /**
  81.      * The thread animating the images.
  82.      */
  83.     Thread kicker = null;
  84.  
  85.     /**
  86.      * The length of the pause between revs.
  87.      */
  88.     int pause;
  89.  
  90.     /**
  91.      * Whether or not the thread has been paused by the user.
  92.      */
  93.     boolean threadSuspended = false;
  94.  
  95.     /**
  96.      * The offscreen image.
  97.      */
  98.     Image    im;
  99.  
  100.     /**
  101.      * The number of images.
  102.      */
  103.     int nimgs;
  104.  
  105.     /**
  106.      * The offscreen graphics context
  107.      */
  108.     Graphics    offscreen;
  109.  
  110.     /**
  111.      * Initialize the applet. Get attributes.
  112.      */
  113.     public void init() {
  114.     String at = getParameter("img");
  115.     dir = (at != null) ? at : "doc:/demo/images/duke";
  116.     at = getParameter("pause");
  117.     pause = (at == null) ? 0 : Integer.valueOf(at).intValue();
  118.     nimgs = Integer.valueOf(getParameter("nimgs")).intValue();
  119.     }
  120.  
  121.     /**
  122.      * Run the image loop. This methods is called by class Thread.
  123.      * @see java.lang.Thread
  124.      */
  125.     public void run() {
  126.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  127.     loop = new ImageLoop(getDocumentBase(), dir, this, nimgs);
  128.  
  129.     if (loop.nimgs > 1) {
  130.         while (size().width > 0 && size().height > 0 && kicker != null) {
  131.         if (++loopslot >= loop.nimgs) {
  132.             loopslot = 0;
  133.         }
  134.         repaint();
  135.         try {
  136.             Thread.sleep(100 + ((loopslot == 0) ? pause : 0));
  137.         } catch (InterruptedException e) {
  138.             return;
  139.         }
  140.         }
  141.     }
  142.     }
  143.  
  144.     /**
  145.      * Paint the current frame.
  146.      */
  147.     public void paint(Graphics g) {
  148.     update(g);
  149.     }
  150.     public void update(Graphics g) {
  151.     if ((loop != null) && (loop.imgs != null) &&
  152.         (loopslot < loop.nimgs) && (loop.imgs[loopslot] != null)) {
  153.         if (im == null) {
  154.         im = createImage(size().width, size().height);
  155.         offscreen = im.getGraphics();
  156.         offscreen.setColor(Color.lightGray);
  157.         }
  158.         offscreen.fillRect(0, 0, size().width, size().height);
  159.         offscreen.drawImage(loop.imgs[loopslot], 0, 0, this);
  160.         g.drawImage(im, 0, 0, this);
  161.     }
  162.     }
  163.  
  164.     /**
  165.      * Start the applet by forking an animation thread.
  166.      */
  167.     public void start() {
  168.     if (kicker == null) {
  169.         kicker = new Thread(this);
  170.         kicker.start();
  171.     }
  172.     }
  173.  
  174.     /**
  175.      * Stop the applet. The thread will exit because kicker is set to null.
  176.      */
  177.     public void stop() {
  178.     kicker = null;
  179.     }
  180.  
  181.     /**
  182.      * Pause the thread when the user clicks the mouse in the applet.
  183.      */
  184.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  185.         if (threadSuspended)
  186.             kicker.resume();
  187.     else
  188.             kicker.suspend();
  189.         threadSuspended = !threadSuspended;
  190.     return true;
  191.     }
  192.  
  193. }
  194.